package com.sukaiyi.giteeideaplugin.externcmd;

import cn.hutool.core.util.StrUtil;
import lombok.Builder;

import java.io.File;

/**
 * @author sukaiyi
 * @date 2020/07/06
 */
@Builder
public class GitCommandExecutor {

    private final String gitExecutablePath;

    public void clone(String url, String dist, CommandExecutor.CommandExecuteListener listener) {
        int lastIndex = url.lastIndexOf('/');
        String projectName = StrUtil.removeSuffix(url.substring(lastIndex + 1), ".git");
        String command = String.format("%s clone %s %s%s%s", gitExecutablePath, url, dist, File.separator, projectName);
        CommandExecutor.exec(command, listener);
    }

    public void cloneAsync(String url, String dist, CommandExecutor.CommandExecuteListener listener) {
        int lastIndex = url.lastIndexOf('/');
        String projectName = StrUtil.removeSuffix(url.substring(lastIndex + 1), ".git");
        String command = String.format("%s clone %s %s%s%s", gitExecutablePath, url, dist, File.separator, projectName);
        CommandExecutor.execAsync(command, listener);
    }
}
